Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enable c api for android ci #1635

Merged
merged 1 commit into from
Dec 20, 2024

Conversation

thewh1teagle
Copy link
Contributor

Resolve #1634

@csukuangfj
Copy link
Collaborator

Thank you for your contribution!

@csukuangfj csukuangfj merged commit 4681bdf into k2-fsa:master Dec 20, 2024
1 check passed
@csukuangfj
Copy link
Collaborator

Will release a new version on the coming Sunday this week.

@adem-rguez
Copy link

hello, great work!
i was wondering, where can i find the c api lib files for android? you added a note for that in release v1.10.36, but after going through android archives i can't find them, i tried using ohos .so files but they didn't work for android :'(..
thank you!

@csukuangfj
Copy link
Collaborator

@adem-rguez
Copy link

thank you, sadly it still didn't work for me, i am trying to do an android build with unity, the moment i call the a function from the dll the app crashes (in an apk android build), do you have any idea why?

@csukuangfj
Copy link
Collaborator

please post error logs.

otherwise, no one except you knows what is happening.

@adem-rguez
Copy link

that's the issue, the app quits, before leaving any log (only log before the the plugin functions is shown), at least that's what i observed from unity, i will see if i can get direct log using logcat

@adem-rguez
Copy link

adem-rguez commented Jan 7, 2025

so here is the logcat output:

2025/01/07 08:10:49.097 19118 21764 Info Unity before Init dll function!
2025/01/07 08:10:49.097 19118 21764 Info Unity  #0 0x7990657d48 (libunity.so) GetStacktrace(int) 0x44
2025/01/07 08:10:49.097 19118 21764 Info Unity  #1 0x7990bb25ac (libunity.so) DebugStringToFile(DebugStringToFileData const&) 0x234
2025/01/07 08:10:49.097 19118 21764 Info Unity  #2 0x7990210190 (libunity.so) DebugLogHandler::Internal_Log(LogType, LogOption, core::basic_string<char, core::StringStorageDefault<char> > const&, Object*) 0x9c
2025/01/07 08:10:49.097 19118 21764 Info Unity  #3 0x79902100a0 (libunity.so) DebugLogHandler_CUSTOM_Internal_Log(LogType, LogOption, ScriptingBackendNativeStringPtrOpaque*, ScriptingBackendNativeObjectPtrOpaque*) 0x128
2025/01/07 08:10:49.097 19118 21764 Info Unity  #4 0x798aba053c (libil2cpp.so) ? 0x0
2025/01/07 08:10:49.097 19118 21764 Info Unity  #5 0x798aaec8d4 (libil2cpp.so) ? 0x0
2025/01/07 08:10:49.097 19118 21764 Info Unity  #6 0x798aaec820 (libil2cpp.so) ? 0x0
2025/01/07 08:10:49.097 19118 21764 Info Unity  #7 0x7990520838 (libunity.so) scripting_method_invoke(ScriptingMethodPtr, ScriptingObjectPtr, ScriptingArguments&, ScriptingExceptionPtr*, bool) 0xb0
2025/01/07 08:10:49.097 19118 21764 Info Unity  #8 0x7990533398 (libunity.so) ScriptingInvocation::Invoke(ScriptingExceptionPtr*, bool) 0x9c
2025/01/07 08:10:49.097 19118 21764 Info Unity  #9 0x799053d154 (libunity.so) MonoBehaviour::InvokeMethodOrCoroutineChecked(ScriptingMethodPtr, ScriptingObjectPtr, ScriptingExceptionPtr*) 0x738
2025/01/07 08:10:49.097 19118 21764 Info Unity  #10 0x799053d37c (libun
2025/01/07 08:10:49.100 19118 21764 Debug nativeloader Load /data/app/~~WyAgEqOao5vbYriEPRFWHA==/com.DefaultCompany.TTSDemo-4rvsvhpnnzXxgQ-XnAidcw==/lib/arm64/libsherpa-onnx-c-api.so using ns clns-4 from class loader (caller=/data/app/~~WyAgEqOao5vbYriEPRFWHA==/com.DefaultCompany.TTSDemo-4rvsvhpnnzXxgQ-XnAidcw==/base.apk): ok
2025/01/07 08:10:49.101 19118 21764 Warn sherpa-onnx OfflineTtsConfig(model=OfflineTtsModelConfig(vits=OfflineTtsVitsModelConfig(model="jar:file:///data/app/~~WyAgEqOao5vbYriEPRFWHA==/com.DefaultCompany.TTSDemo-4rvsvhpnnzXxgQ-XnAidcw==/base.apk!/assets/vits-piper-en_US-libritts_r-medium/en_US-libritts_r-medium.onnx", lexicon="", tokens="jar:file:///data/app/~~WyAgEqOao5vbYriEPRFWHA==/com.DefaultCompany.TTSDemo-4rvsvhpnnzXxgQ-XnAidcw==/base.apk!/assets/vits-piper-en_US-libritts_r-medium/tokens.txt", data_dir="jar:file:///data/app/~~WyAgEqOao5vbYriEPRFWHA==/com.DefaultCompany.TTSDemo-4rvsvhpnnzXxgQ-XnAidcw==/base.apk!/assets/espeak-ng-data", dict_dir="", noise_scale=0.667, noise_scale_w=0.8, length_scale=1), matcha=OfflineTtsMatchaModelConfig(acoustic_model="", vocoder="", lexicon="", tokens="", data_dir="", dict_dir="", noise_scale=0.667, length_scale=1), num_threads=1, debug=True, provider="cpu"), rule_fsts="", rule_fars="", max_num_sentences=5)
2025/01/07 08:10:49.101 19118 21764 Warn sherpa-onnx --vits-model: 'jar:file:///data/app/~~WyAgEqOao5vbYriEPRFWHA==/com.DefaultCompany.TTSDemo-4rvsvhpnnzXxgQ-XnAidcw==/base.apk!/assets/vits-piper-en_US-libritts_r-medium/en_US-libritts_r-medium.onnx' does not exist
2025/01/07 08:10:49.101 19118 21764 Warn sherpa-onnx Errors in config
2025/01/07 08:10:49.106 19118 21764 Error CRASH *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
2025/01/07 08:10:49.106 19118 21764 Error CRASH Version '2022.3.55f1 (9f374180d209)', Build type 'Development', Scripting Backend 'il2cpp', CPU 'arm64-v8a'
2025/01/07 08:10:49.106 19118 21764 Error CRASH Build fingerprint: 'OPPO/CPH2371EEA/OPD4A1L1:13/TP1A.220905.001/R.1ade725-3a70:user/release-keys'
2025/01/07 08:10:49.106 19118 21764 Error CRASH Revision: '0'
2025/01/07 08:10:49.106 19118 21764 Error CRASH ABI: 'arm64'
2025/01/07 08:10:49.106 19118 21764 Error CRASH Timestamp: 2025-01-07 08:10:49.106611479+0100
2025/01/07 08:10:49.106 19118 21764 Error CRASH pid: 19118, tid: 21764, name: UnityMain  >>> com.DefaultCompany.TTSDemo <<<
2025/01/07 08:10:49.106 19118 21764 Error CRASH uid: 10503
2025/01/07 08:10:49.106 19118 21764 Error CRASH signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr --------
2025/01/07 08:10:49.106 19118 21764 Error CRASH Cause: null pointer dereference
2025/01/07 08:10:49.106 19118 21764 Error CRASH     x0  0000000000000000  x1  000000785829cc70  x2  0000007858200000  x3  0000000000000003
2025/01/07 08:10:49.106 19118 21764 Error CRASH     x4  000000000000009c  x5  09060c0606000000  x6  00008006060c0609  x7  6f71646751ff6473
2025/01/07 08:10:49.106 19118 21764 Error CRASH     x8  000000781cdfe048  x9  625105d9b302f096  x10 000000000000009c  x11 0000000000000001
2025/01/07 08:10:49.106 19118 21764 Error CRASH     x12 0000000000000004  x13 09b3ddf772f3d8ef  x14 0000000000000006  x15 ffffffffffffffff
2025/01/07 08:10:49.106 19118 21764 Error CRASH     x16 0000007ab6d098f0  x17 0000007ab6cf9800  x18 0000007940e9e138  x19 0000000000000000
2025/01/07 08:10:49.106 19118 21764 Error CRASH     x20 000000798bdda000  x21 000000798bcf7108  x22 0000007a06dc4960  x23 000000798bcf5e58
2025/01/07 08:10:49.106 19118 21764 Error CRASH     x24 000000798bd10f00  x25 000000798bcf7d90  x26 000000798bd1f458  x27 0000007a06dc48d0
2025/01/07 08:10:49.106 19118 21764 Error CRASH     x28 0000007a06dc4838  x29 0000007a06dc4950
2025/01/07 08:10:49.106 19118 21764 Error CRASH     lr  000000798aba1fd0  sp  0000007a06dc4740  pc  000000781cdfe048  pst 0000000060001000
2025/01/07 08:10:49.106 19118 21764 Error CRASH backtrace:
2025/01/07 08:10:49.106 19118 21764 Error CRASH       #00 pc 00000000001cc048  /data/app/~~WyAgEqOao5vbYriEPRFWHA==/com.DefaultCompany.TTSDemo-4rvsvhpnnzXxgQ-XnAidcw==/lib/arm64/libsherpa-onnx-c-api.so (SherpaOnnxOfflineTtsSampleRate) (BuildId: 085f02850ae50e2fbf8897e1b2f65e3f4ce7f8c1)
2025/01/07 08:10:49.106 19118 21764 Error CRASH       #01 pc 0000000000d62fcc  /data/app/~~WyAgEqOao5vbYriEPRFWHA==/com.DefaultCompany.TTSDemo-4rvsvhpnnzXxgQ-XnAidcw==/lib/arm64/libil2cpp.so (BuildId: e89cd621ac49346ff2352b397db175980a63ca83)
2025/01/07 08:10:49.106 19118 21764 Error CRASH       #02 pc 0000000000d61590  /data/app/~~WyAgEqOao5vbYriEPRFWHA==/com.DefaultCompany.TTSDemo-4rvsvhpnnzXxgQ-XnAidcw==/lib/arm64/libil2cpp.so (BuildId: e89cd621ac49346ff2352b397db175980a63ca83)
2025/01/07 08:10:49.106 19118 21764 Error CRASH       #03 pc 0000000000cad8d4  /data/app/~~WyAgEqOao5vbYriEPRFWHA==/com.DefaultCompany.TTSDemo-4rvsvhpnnzXxgQ-XnAidcw==/lib/arm64/libil2cpp.so (BuildId: e89cd621ac49346ff2352b397db175980a63ca83)
2025/01/07 08:10:49.106 19118 21764 Error CRASH       #04 pc 0000000000cad820  /data/app/~~WyAgEqOao5vbYriEPRFWHA==/com.DefaultCompany.TTSDemo-4rvsvhpnnzXxgQ-XnAidcw==/lib/arm64/libil2cpp.so (BuildId: e89cd621ac49346ff2352b397db175980a63ca83)
2025/01/07 08:10:49.107 19118 21764 Error CRASH       #05 pc 00000000006b0838  /data/app/~~WyAgEqOao5vbYriEPRFWHA==/com.DefaultCompany.TTSDemo-4rvsvhpnnzXxgQ-XnAidcw==/lib/arm64/libunity.so (scripting_method_invoke(ScriptingMethodPtr, ScriptingObjectPtr, ScriptingArguments&, ScriptingExceptionPtr*, bool)+176) (BuildId: b2cc86a340fea31d)
2025/01/07 08:10:49.107 19118 21764 Error CRASH       #06 pc 00000000006c3398  /data/app/~~WyAgEqOao5vbYriEPRFWHA==/com.DefaultCompany.TTSDemo-4rvsvhpnnzXxgQ-XnAidcw==/lib/arm64/libunity.so (ScriptingInvocation::Invoke(ScriptingExceptionPtr*, bool)+156) (BuildId: b2cc86a340fea31d)
2025/01/07 08:10:49.107 19118 21764 Error CRASH       #07 pc 00000000006cd154  /data/app/~~WyAgEqOao5vbYriEPRFWHA==/com.DefaultCompany.TTSDemo-4rvsvhpnnzXxgQ-XnAidcw==/lib/arm64/libunity.so (MonoBehaviour::InvokeMethodOrCoroutineChecked(ScriptingMethodPtr, ScriptingObjectPtr, ScriptingExceptionPtr*)+1848) (BuildId: b2cc86a340fea31d)
2025/01/07 08:10:49.107 19118 21764 Error CRASH       #08 pc 00000000006cd37c  /data/app/~~WyAgEqOao5vbYriEPRFWHA==/com.DefaultCompany.TTSDemo-4rvsvhpnnzXxgQ-XnAidcw==/lib/arm64/libunity.so (MonoBehaviour::InvokeMethodOrCoroutineChecked(ScriptingMethodPtr, ScriptingObjectPtr)+68) (BuildId: b2cc86a340fea31d)
2025/01/07 08:10:49.107 19118 21764 Error CRASH       #09 pc 00000000006cdae0  /data/app/~~WyAgEqOao5vbYriEPRFWHA==/com.DefaultCompany.TTSDemo-4rvsvhpnnzXxgQ-XnAidcw==/lib/arm64/libunity.so (MonoBehaviour::DelayedStartCall(Object*, void*)+48) (BuildId: b2cc86a340fea31d)
2025/01/07 08:10:49.107 19118 21764 Error CRASH       #10 pc 00000000004a6bd8  /data/app/~~WyAgEqOao5vbYriEPRFWHA==/com.DefaultCompany.TTSDemo-4rvsvhpnnzXxgQ-XnAidcw==/lib/arm64/libunity.so (DelayedCallManager::Update(int)+560) (BuildId: b2cc86a340fea31d)
2025/01/07 08:10:49.107 19118 21764 Error CRASH       #11 pc 0000000000568294  /data/app/~~WyAgEqOao5vbYriEPRFWHA==/com.DefaultCompany.TTSDemo-4rvsvhpnnzXxgQ-XnAidcw==/lib/arm64/libunity.so (BuildId: b2cc86a340fea31d)
2025/01/07 08:10:49.107 19118 21764 Error CRASH       #12 pc 000000000055df38  /data/app/~~WyAgEqOao5vbYriEPRFWHA==/com.DefaultCompany.TTSDemo-4rvsvhpnnzXxgQ-XnAidcw==/lib/arm64/libunity.so (ExecutePlayerLoop(NativePlayerLoopSystem*)+132) (BuildId: b2cc86a340fea31d)
2025/01/07 08:10:49.107 19118 21764 Error CRASH       #13 pc 000000000055df78  /data/app/~~WyAgEqOao5vbYriEPRFWHA==/com.DefaultCompany.TTSDemo-4rvsvhpnnzXxgQ-XnAidcw==/lib/arm64/libunity.so (ExecutePlayerLoop(NativePlayerLoopSystem*)+196) (BuildId: b2cc86a340fea31d)
2025/01/07 08:10:49.107 19118 21764 Error CRASH       #14 pc 000000000055e290  /data/app/~~WyAgEqOao5vbYriEPRFWHA==/com.DefaultCompany.TTSDemo-4rvsvhpnnzXxgQ-XnAidcw==/lib/arm64/libunity.so (PlayerLoop()+324) (BuildId: b2cc86a340fea31d)
2025/01/07 08:10:49.107 19118 21764 Error CRASH       #15 pc 0000000000775b38  /data/app/~~WyAgEqOao5vbYriEPRFWHA==/com.DefaultCompany.TTSDemo-4rvsvhpnnzXxgQ-XnAidcw==/lib/arm64/libunity.so (UnityPlayerLoop()+836) (BuildId: b2cc86a340fea31d)
2025/01/07 08:10:49.107 19118 21764 Error CRASH       #16 pc 0000000000793f08  /data/app/~~WyAgEqOao5vbYriEPRFWHA==/com.DefaultCompany.TTSDemo-4rvsvhpnnzXxgQ-XnAidcw==/lib/arm64/libunity.so (nativeRender(_JNIEnv*, _jobject*)+84) (BuildId: b2cc86a340fea31d)
2025/01/07 08:10:49.107 19118 21764 Error CRASH       #17 pc 0000000000383d70  /apex/com.android.art/lib64/libart.so (BuildId: c35c9ebf7bb06435e4b31977d87bd5d5)
2025/01/07 08:10:49.107 19118 21764 Error CRASH       #18 pc 000000000036d574  /apex/com.android.art/lib64/libart.so (BuildId: c35c9ebf7bb06435e4b31977d87bd5d5)
2025/01/07 08:10:49.107 19118 21764 Error CRASH       #19 pc 0000000000366a80  /apex/com.android.art/lib64/libart.so (BuildId: c35c9ebf7bb06435e4b31977d87bd5d5)
2025/01/07 08:10:49.107 19118 21764 Error CRASH       #20 pc 000000000076e870  /apex/com.android.art/lib64/libart.so (BuildId: c35c9ebf7bb06435e4b31977d87bd5d5)
2025/01/07 08:10:49.107 19118 21764 Error CRASH       #21 pc 00000000003863d8  /apex/com.android.art/lib64/libart.so (BuildId: c35c9ebf7bb06435e4b31977d87bd5d5)
2025/01/07 08:10:49.107 19118 21764 Error CRASH       #22 pc 0000000000358eec  /apex/com.android.art/lib64/libart.so (BuildId: c35c9ebf7bb06435e4b31977d87bd5d5)
2025/01/07 08:10:49.107 19118 21764 Error CRASH       #23 pc 0000000000367314  /apex/com.android.art/lib64/libart.so (BuildId: c35c9ebf7bb06435e4b31977d87bd5d5)
2025/01/07 08:10:49.107 19118 21764 Error CRASH       #24 pc 000000000076e870  /apex/com.android.art/lib64/libart.so (BuildId: c35c9ebf7bb06435e4b31977d87bd5d5)
2025/01/07 08:10:49.107 19118 21764 Error CRASH       #25 pc 00000000003863d8  /apex/com.android.art/lib64/libart.so (BuildId: c35c9ebf7bb06435e4b31977d87bd5d5)
2025/01/07 08:10:49.107 19118 21764 Error CRASH       #26 pc 0000000000358278  /apex/com.android.art/lib64/libart.so (BuildId: c35c9ebf7bb06435e4b31977d87bd5d5)
2025/01/07 08:10:49.107 19118 21764 Error CRASH       #27 pc 0000000000383e98  /apex/com.android.art/lib64/libart.so (BuildId: c35c9ebf7bb06435e4b31977d87bd5d5)
2025/01/07 08:10:49.107 19118 21764 Error CRASH       #28 pc 00000000020c03e4  /memfd:jit-cache (deleted)
2025/01/07 08:10:49.250 19118 21764 Error CRASH Tombstone written to: /storage/emulated/0/Android/data/com.DefaultCompany.TTSDemo/files/tombstone_00
2025/01/07 08:10:49.250 19118 21764 Error CRASH Forwarding signal 11
0001/01/01 00:00:00.000 -1 -1 Info  --------- beginning of crash
2025/01/07 08:10:49.250 19118 21764 Fatal libc Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x0 in tid 21764 (UnityMain), pid 19118 (Company.TTSDemo)

here is also the part of the code that calls that function (the crash happens when i call SherpaOnnxCreateOfflineTts but i assume it would be the same for the rest of the functions as well):
image

plugins placement in unity:
image

and their import settings (the same for the 3 .so files):
image

i will be waiting for you reply or if you need further details, thank you!

@csukuangfj
Copy link
Collaborator

csukuangfj commented Jan 7, 2025

from the log
2025/01/07 08:10:49.101 19118 21764 Warn sherpa-onnx --vits-model: 'jar:file:///data/app/~~WyAgEqOao5vbYriEPRFWHA==/com.DefaultCompany.TTSDemo-4rvsvhpnnzXxgQ-XnAidcw==/base.apk!/assets/vits-piper-en_US-libritts_r-medium/en_US-libritts_r-medium.onnx' does not exist

Please check that the file is placed in the correct location.

@adem-rguez
Copy link

i see, the file is indeed placed correctly, but now i understand the problem, unity packs the location its in (StreamingAssets folder) differently in android, and IO operations won't be able to access it like that.. this is bad, i will need to find an alternative way to load the models on android

@csukuangfj
Copy link
Collaborator

2025/01/07 08:10:49.101 19118 21764 Warn sherpa-onnx OfflineTtsConfig(model=OfflineTtsModelConfig(vits=OfflineTtsVitsModelConfig(model="jar:file:///data/app/~~WyAgEqOao5vbYriEPRFWHA==/com.DefaultCompany.TTSDemo-4rvsvhpnnzXxgQ-XnAidcw==/base.apk!/assets/vits-piper-en_US-libritts_r-medium/en_US-libritts_r-medium.onnx", lexicon="", tokens="jar:file:///data/app/~~WyAgEqOao5vbYriEPRFWHA==/com.DefaultCompany.TTSDemo-4rvsvhpnnzXxgQ-XnAidcw==/base.apk!/assets/vits-piper-en_US-libritts_r-medium/tokens.txt", data_dir="jar:file:///data/app/~~WyAgEqOao5vbYriEPRFWHA==/com.DefaultCompany.TTSDemo-4rvsvhpnnzXxgQ-XnAidcw==/base.apk!/assets/espeak-ng-data", dict_dir="", noise_scale=0.667, noise_scale_w=0.8, length_scale=1), matcha=OfflineTtsMatchaModelConfig(acoustic_model="", vocoder="", lexicon="", tokens="", data_dir="", dict_dir="", noise_scale=0.667, length_scale=1), num_threads=1, debug=True, provider="cpu"), rule_fsts="", rule_fars="", max_num_sentences=5)

Here is the log from sherpa-onnx.

It shows you have indeed passed some parameters to sherpa-onnx's C API.

Please pay attention to the path.

jar:file:///data/app/

The C++ code uses fopen and fread-like functions to read files.

I don't know what the meaning of jar:file://

@csukuangfj
Copy link
Collaborator

StreamingAssets folder) differently in android, and IO operations won't be able to access it like that..

Can you first copy the files from assets into disk?

@csukuangfj
Copy link
Collaborator

Or does unity provide C or C++ APIs to read files from assets? If so, we can change sherpa-onnx to support that as long as you can help to test the changes.

@adem-rguez
Copy link

adem-rguez commented Jan 7, 2025

i think it's possible using AndroidJavaClass class in unity, but the easiest way would be to copy the models into a readable location once on android, there is way to load such files in unity using UnityWebRequest, load them from the StreamingAssets location, and copy them somewhere differently (on a readable path), then i can pass the new path to the sherpa plugin, and this has to be done only the first time you open the apk, this is the best work-around usually, if you want i can provide the code i will write for that later :)

@csukuangfj
Copy link
Collaborator

Once it works for you, it would be great if you can post the code here so that others won't get the same issues.

@adem-rguez
Copy link

i am unsure about the espeak data path however, how is the plugin reading it?

@csukuangfj
Copy link
Collaborator

It is the same as the model, e.g., the same as tokens.txt

Just copy the folder from the assets to some locations and pass the path to sherpa-onnx.

@csukuangfj
Copy link
Collaborator

By the way, you need to copy all the files from espeak-ng-data directory and you need to keep the structure of epseak-ng-data.

espeak-ng-data also contains sub-directories.

@adem-rguez
Copy link

okay, working on it

@adem-rguez
Copy link

Good news! i got it to work, here is an apk to test, i will provide a cleaned version of the code involved later in the day, and hopefully make an asset store package after that with more automated features like voice models download and easy integration of sherpa-onnx in unity, having packages for this in game engines such as unity and unreal engine is the way to make this super project super popular! great work @csukuangfj!
apk arm64: https://drive.google.com/file/d/1-xcQAMGCp-6fiwR8KuY1fJvmuN0wn0Pv/view?usp=sharing

@csukuangfj
Copy link
Collaborator

Thanks!

@adem-rguez
Copy link

question: is token.txt file common for all models, or does each model have a separate one?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add release for android C API
3 participants